package com.tanhua.server.controller;


import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.concurrent.TimeUnit;


@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 完善个人信息
     */
    @RequestMapping("/loginReginfo")
    @PostMapping
    public ResponseEntity<Object> saveUserInfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {
        User user = userService.queryUserByToken(token);
        userInfo.setId(user.getId());
        return userService.saveUserInfo(userInfo);
    }

    /**
     * 上传头像
     */
    @RequestMapping("loginReginfo/head")
    @PostMapping
    public ResponseEntity<Object> saveLogo(@RequestParam("headPhoto") MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws IOException {
        User user = userService.queryUserByToken(token);
        return userService.saveLogo(headPhoto,user);
    }


    /**
     * 根据token查询用户数据
     */
    @GetMapping("{token}")
    public User queryUserByToken(@PathVariable("token") String token) {
       return userService.queryUserByToken(token);
    }
}
